<?php

	class Product extends CI_Model
	{
		function __construct()
		{
			parent::__construct();
		}
				
		function getProduct($id = false)
		{
			$query = $this->db->get_where('tghm_products',array('id'=>$id));
			return $query->row_array();
		}		

		function getAllProducts($cat_id, $num_of_products, $limit)
		{
			$this->db->select('*');
			$this->db->from('tghm_products');
			
			if( $cat_id > 0 )
			{				
				$this->db->where('category_id', $cat_id);
			}			

			$this->db->order_by('id','desc');

			if($num_of_products)
			{
				$this->db->limit($limit, $num_of_products);
			}
			else
			{
				$this->db->limit($limit);
			}
			
			$query = $this->db->get();
			return $query->result_array();
		}

		function getNewProducts()
		{
			$this->db->select('*');
			$this->db->from('tghm_products');
			$this->db->order_by('id','desc');
			$this->db->limit(9);

			$query = $this->db->get();
			return $query->result_array();
		}
		
		function getNewProducts1($cat_id = false,$num = false)
		{
			$this->db->select('*');
			$this->db->from('tghm_products');
			$this->db->where(array('category_id'=>$cat_id));
			$this->db->order_by('id','desc');			
			$this->db->limit($num);

			$query = $this->db->get();
			return $query->result_array();
		}

		function addNewProduct($data)
		{
			return $this->db->insert('tghm_products',$data);	
		}

		function editProduct($data = false,$id = false)
		{
			$this->db->where('id',$id);
			$this->db->update('tghm_products',$data);
		}

		function search($data = false,$page = false)
		{
			//search keyword,price,price to
			$this->db->select('*');
			$this->db->from('tghm_products');
			if(isset($data['search']['keyword']) && !empty($data['search']['keyword']))
			{
				$this->db->like('name',$data['search']['keyword']);	
			}
			if(isset($data['search']['price']) && !empty($data['search']['price']))
			{
				$this->db->where('price >',$data['search']['price']);
			}
			if(isset($data['search']['price_to']) && !empty($data['search']['price_to']))
			{
				$this->db->where('price <',$data['search']['price_to']);	
			}

			if($page)
			{
				$this->db->limit(10,$page);
			}
			else
			{
				$this->db->limit(10,0);
			}
			
			$query = $this->db->get();
			
			return $query->result_array();
		}
		
		// dem so record trong bang
		function count_all_results()
		{
			return $this->db->count_all('tghm_products');
		}

		// count records with condition [category_id]
		function count_results($category_id)
		{
			$this->db->get_where('tghm_products',array('category_id'=>$category_id));
			return $this->db->count_all_results();
		}

		function deleteProduct($id)
		{
			$this->db->delete('tghm_products',array('id'=>$id));
		}

		// retrieve list of products has same category
		function getProductsSameCat($id)
		{
			// get category_id from product ID
			$query = $this->db->get_where('tghm_products',array('id'=>$id),1);

			if( $query->num_rows() > 0 )
			{
				$row = $query->row();

				$category_id = $row->category_id;

				if( $category_id > 0 )
				{
					// retrieve list of products 
					$query = $this->db->get_where('tghm_products',array('category_id'=>$category_id,'id !='=>$id));

					return $query->result_array();	
				}
			}
			return false;
		}
	}